/**
 * @program: pettyloan
 * @ClassName Repayment
 * @author: 汪江 四种还款方式的计算工具类
 * @create: 2022-12-06 15:49
 **/
package com.wn.pettyloan.util;

import com.wn.pettyloan.util.utilentity.RePay;
import com.wn.pettyloan.util.utilentity.RepayPlan;

public class Repayment {
    //等本等息
    public static RePay method1(RepayPlan repayPlan) {
        RePay rePay = new RePay();
        rePay.setPrincipal(repayPlan.getPrincipal() / repayPlan.getSumMonth());
        rePay.setInterest(repayPlan.getPrincipal() * repayPlan.getInterestRate() / 12);
        rePay.setTotal(rePay.getPrincipal() + rePay.getInterest());
        return rePay;
    }

    //等额本息
    public static RePay method2(RepayPlan repayPlan) {
        RePay rePay = new RePay();
        double pow = Math.pow((1 + repayPlan.getInterestRate() / 12), repayPlan.getSumMonth());
        double v = (repayPlan.getPrincipal() * (repayPlan.getInterestRate() / 12) * pow) / (pow - 1);
        rePay.setTotal(v);
        rePay.setInterest((repayPlan.getPrincipal() - repayPlan.getAlreadyRepy()) * repayPlan.getInterestRate() / 12);
        rePay.setPrincipal(rePay.getTotal() - rePay.getInterest());
        return rePay;
    }

    //等额本金
    public static RePay method3(RepayPlan repayPlan) {
        RePay rePay = new RePay();
        rePay.setPrincipal(repayPlan.getPrincipal() / repayPlan.getSumMonth());
        rePay.setInterest((repayPlan.getPrincipal() - repayPlan.getAlreadyRepy()) * (repayPlan.getInterestRate() / 12));
        rePay.setTotal(rePay.getPrincipal() + rePay.getInterest());
        return rePay;
    }

    //先息后本
    public static RePay method4(RepayPlan repayPlan) {
        RePay rePay = new RePay();
        if (repayPlan.getNowMonth() == repayPlan.getSumMonth()) {
            rePay.setPrincipal(repayPlan.getPrincipal());
        } else {
            rePay.setPrincipal(0);
        }
        rePay.setInterest(repayPlan.getPrincipal() * repayPlan.getInterestRate() / 12);
        rePay.setTotal(rePay.getPrincipal() + rePay.getInterest());
        return rePay;
    }
}

